﻿#include <libcomm.h>

ThreadSignal m_exitSignal;
#ifndef COMM_MSVC
void __SignalHandler(int signo) 
{
	printf("SIGNAL(%d) interrupt !\n",signo);
	if(SIGINT == signo)
	{
		m_exitSignal.SetSignal();
		return;
	}
	printf("%s",StackTracer::TraceStack().get());
	exit(0);
}
#endif

#define CRT_FILE	"/www.wanghonghao.cn.pem"
#define KEY_FILE	"/www.wanghonghao.cn.key"


class MyHttpRequestHandler : public HttpRequestHandler
{
public:
	virtual bool OnRequest(const char* uri, const char *cmdType, const StringList& requestHeaders, const char* requestContent,
		StringList& responseHeaders, Buffer& responseContent)
	{
		static AtomInt j;
		int i = j++;
		printf("%03d.(%s)...\n", i,uri);
		responseHeaders << "Server: redrat";
		responseHeaders << "Content-Type: text/html;charset=gb2312";
		//responseContent = "hello, every body ! coming soon !";
		return true;
	}
};

int main(int argc, char *argv[])
{
#ifndef COMM_MSVC
	struct sigaction act;
	act.sa_handler = __SignalHandler;
	MYLIB_ASSERT_ERRNO(0==sigfillset(&act.sa_mask));//forbid all signal while handler.
	act.sa_flags = 0;
	MYLIB_ASSERT_ERRNO(0==sigaction(SIGINT,&act,NULL));
	MYLIB_ASSERT_ERRNO(0==sigaction(SIGSEGV,&act,NULL));
	MYLIB_ASSERT_ERRNO(0==sigaction(SIGILL,&act,NULL));
	MYLIB_ASSERT_ERRNO(0==sigaction(SIGFPE,&act,NULL));
	MYLIB_ASSERT_ERRNO(0==sigaction(SIGTERM,&act,NULL));
	MYLIB_ASSERT_ERRNO(0==sigaction(SIGABRT,&act,NULL));
	//signal(SIGUSR1,SIG_IGN);
#endif

	LOGDEBUG("COMLIB: %s",common_version());

	String currentdir = Application::GetApplicationDirectory();
	HttpServer server(new MyHttpRequestHandler(), 80, "0.0.0.0", 20, 8, 2/*,currentdir+CRT_FILE,currentdir+KEY_FILE*/);
	server.Start();
	//m_exitSignal.WaitSignal();
	getchar();
	server.Stop();

	return 0;
}

// webbench -f -c 20 -t 60 http://127.0.0.1/index